Skip to content

fix: align CrossJoinExec schema metadata merge with the logical plan#23442

Open
fangchenli wants to merge 3 commits into
apache:mainfrom
fangchenli:fix/cross-join-schema-metadata
Open

fix: align CrossJoinExec schema metadata merge with the logical plan#23442
fangchenli wants to merge 3 commits into
apache:mainfrom
fangchenli:fix/cross-join-schema-metadata

Conversation

@fangchenli

@fangchenli fangchenli commented Jul 10, 2026

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

A cross join over two inputs whose schemas share a metadata key with different
values produced a physical schema that disagreed with the logical plan, so when
the cross join fed an aggregate the planner failed with "Physical input schema
should be the same as the one converted from logical input schema".

The logical plan and the shared build_join_schema helper merge inner-join
metadata left-biased, but CrossJoinExec::new merged its own schema right-biased
(left.metadata().extend(right)). #16221 fixed the shared helper for hash and
nested-loop joins but never touched cross join.

What changes are included in this PR?

Route CrossJoinExec::new through the shared build_join_schema helper so its
metadata merges the same way as the logical plan. Field output is unchanged for
inner joins.

Known remaining gap (pre-existing, shared with hash/NLJ): under join reordering, the swap path still flips a conflicting schema-level metadata key; tracked in #23461.

Are these changes tested?

Yes, a metadata.slt regression that cross-joins two tables with conflicting
schema metadata into an aggregate. It failed with the internal error before and
passes after.

Are there any user-facing changes?

Bug fix only: affected cross joins now plan and run instead of erroring. No API
changes.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) physical-plan Changes to the physical-plan crate labels Jul 10, 2026

@nathanb9 nathanb9 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the change. The core fix looks right: for JoinType::Inner the shared build_join_schema produces a byte-identical field list to the old hand-rolled code, so only the metadata precedence changes to left-wins, matching the logical plan. Proto roundtrip re-derives the schema via CrossJoinExec::new, so no compat issue there.

A few comments inline, one about a remaining gap worth documenting.

FYI: AI assisted comments - but ive reviewed / verified each 👌

Comment thread datafusion/physical-plan/src/joins/cross_join.rs
Comment thread datafusion/sqllogictest/test_files/metadata.slt
Comment thread datafusion/physical-plan/src/joins/cross_join.rs
fangchenli and others added 2 commits July 10, 2026 11:59
`CrossJoinExec::new` merged schema-level metadata right-biased
(`left.metadata().extend(right)`), while the logical plan and the shared
physical `build_join_schema` are left-biased for inner joins. When both
inputs carry the same metadata key with different values, the cross join's
physical schema diverged from the logical one and tripped the physical
planner's `schema_satisfied_by` check (raised when the cross join feeds an
aggregate), failing with an internal error.

Route `CrossJoinExec::new` through the shared `build_join_schema` helper
(the one apache#16221 fixed for hash/nested-loop joins but never applied to cross
join) so metadata is merged consistently. Field output is unchanged for
inner joins.

Adds a unit test and a metadata.slt regression over two tables whose
schema metadata conflicts under the same key.

Closes apache#23434

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add a metadata.slt variant that runs the cross join regression with
`join_reordering = false`, so the fix is pinned on the initial (non-swapped)
cross join plan and stays insulated from the separate pre-existing swap-path
divergence.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CrossJoinExec merges schema metadata inconsistently with the logical plan

2 participants